package com.tbit.main.pojo.maintain_performance.group;

import com.tbit.main.constant.enums.ConditionLogic;
import com.tbit.main.constant.enums.PerformanceBizType;
import com.tbit.main.constant.enums.PerformanceRuleType;
import com.tbit.main.constant.enums.RuleLogic;
import com.tbit.main.pojo.PerformanceRule;
import com.tbit.main.pojo.PerformanceRuleCondition;
import com.tbit.main.pojo.PerformanceRuleGroup;
import com.tbit.main.pojo.maintain_performance.BizRuleGroup;
import com.tbit.main.pojo.maintain_performance.rule.RepairRule;
import lombok.Data;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * 维修类型绩效规则组
 *
 * @author <a href="mailto:zheng.zebiao@tbit.com.cn">zb</a>
 * @since 2025/2/24
 */
@Data
public class RepairGroup implements BizRuleGroup {
    /**
     * 是否启用
     */
    private Boolean enabled;

    /**
     * 维修类型绩效规则
     */
    private List<RepairRule> subRules;

    @Override
    public PerformanceRuleGroup toRaw(PerformanceBizType bizType, Integer accountId) {
        PerformanceRuleGroup group = new PerformanceRuleGroup();
        group.setAccountId(accountId);
        group.setBizType(bizType);
        group.setRuleType(PerformanceRuleType.REPAIR_TYPE);
        group.setEnabled(enabled);
        group.setRules(new ArrayList<>());

        if (subRules == null) {
            return group;
        }
        for (RepairRule rule : subRules) {
            PerformanceRule performanceRule = new PerformanceRule();
            performanceRule.setPerformance(rule.getPerformance());
            performanceRule.setConditionLogic(ConditionLogic.OR);
            performanceRule.setConditions(new ArrayList<>());

            // 维修类型条件
            for (Integer type : rule.getRepairTypes()) {
                PerformanceRuleCondition condition = new PerformanceRuleCondition();
                condition.setKey("repairType");
                condition.setLogic(RuleLogic.EQUAL);
                condition.setValue(type.toString());
                performanceRule.getConditions().add(condition);
            }
            group.getRules().add(performanceRule);
        }
        return group;
    }

    @Override
    public void initFromRaw(PerformanceRuleGroup group) {
        setEnabled(group.getEnabled());
        setSubRules(new ArrayList<>());
        if (group.getRules() == null) {
            return;
        }
        for (PerformanceRule rule : group.getRules()) {
            RepairRule subRule = new RepairRule();
            subRule.setPerformance(rule.getPerformance());
            subRule.setRepairTypes(new HashSet<>());
            if (rule.getConditions() != null) {
                for (PerformanceRuleCondition condition : rule.getConditions()) {
                    if ("repairType".equals(condition.getKey())) {
                        subRule.getRepairTypes().add(Integer.parseInt(condition.getValue()));
                    }
                }
            }
            getSubRules().add(subRule);
        }
    }
}
